package com.ben.multiple.poi;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * @author BenJin Yin
 * @date 2020/6/30
 */
public class MultipleRest {
    static final String URL_PREFIX = "http://localhost:9009/multi";
    static TestRestTemplate restTemplate;

    @BeforeAll
    static void init() {
        restTemplate = new TestRestTemplate(new RestTemplateBuilder());
    }

    @Test
    void array() {
        String url = URL_PREFIX + "/array";
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("files", new FileSystemResource(new File("D:\\1.xls")));
        map.add("files", new FileSystemResource(new File("D:\\2.xls")));
        map.add("files", new FileSystemResource(new File("D:\\3.xls")));
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(map, httpHeaders);
        ResponseEntity<String> entity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
        this.print(entity);
    }

    @Test
    void dive() {
        String url = URL_PREFIX + "/dive";
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("file1", new FileSystemResource(new File("D:\\1.xls")));
        map.add("file2", new FileSystemResource(new File("D:\\2.xls")));
        map.add("file3", new FileSystemResource(new File("D:\\3.xls")));
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(map, httpHeaders);
        ResponseEntity<String> entity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
        this.print(entity);
    }


    void print(ResponseEntity<?> entity) {
        if (entity == null || HttpStatus.OK != entity.getStatusCode()) {
            System.out.println(entity);
            return;
        }
        System.out.println(entity.getBody());
    }
}
